package com.example.teacher.Converter;

import com.example.common.domain.pojo.Question;
import com.example.common.domain.vo.QuestionVo;
import com.example.teacher.domain.dto.QuestionSaveDto;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class QuestionConverter {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static List<Question> convertToQuestionList(List<QuestionSaveDto> question) {
        return question.stream()
                .map(QuestionConverter::convertToQuestion)
                .toList();
    }

    /**
     * 将 QuestionSaveDto 转换为 Question 实体
     */
    public static Question convertToQuestion(QuestionSaveDto dto) {
        if (dto == null) {
            return null;
        }
        Question question = new Question();
        // 直接复制基本字段
        question.setId(dto.getId());
        question.setQuestionText(dto.getQuestionText());
        question.setCourseId(dto.getCourseId());
        question.setType(dto.getType());
        question.setDifficulty(dto.getDifficulty());
        question.setExplanation(dto.getExplanation());
        question.setScore(dto.getScore());
        question.setRemarks(dto.getRemarks());
        question.setMediaUrl(dto.getMediaUrl());
        question.setCreatedAt(dto.getCreatedAt());
        question.setUpdatedAt(dto.getUpdatedAt());

        // 转换集合字段为 JSON 字符串
        question.setOptions(convertMapToJson(dto.getOptions()));
        question.setCorrectAnswers(convertListToJson(dto.getCorrectAnswers()));
        question.setKnowledgePoints(convertListToJson(dto.getKnowledgePoints()));

        return question;
    }

    /**
     * 将 Map 转换为 JSON 字符串
     */
    private static String convertMapToJson(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        try {
            return objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("map转化json失败", e);
        }
    }

    /**
     * 将 List 转换为 JSON 字符串
     */
    private static String convertListToJson(List<String> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        try {
            return objectMapper.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("list转化json失败", e);
        }
    }

    /**
     * Question 转 QuestionVo
     */
    public QuestionVo convertToQuestionVo(Question question) {
        if (question == null) {
            return null;
        }

        QuestionVo vo = new QuestionVo();
        vo.setId(question.getId());
        vo.setQuestionText(question.getQuestionText());
        vo.setCourseId(question.getCourseId());
        vo.setType(question.getType());
        vo.setDifficulty(question.getDifficulty());
        vo.setExplanation(question.getExplanation());
        vo.setScore(question.getScore());
        vo.setRemarks(question.getRemarks());
        vo.setMediaUrl(question.getMediaUrl());
        vo.setCreatedAt(question.getCreatedAt());
        vo.setUpdatedAt(question.getUpdatedAt());

        // 通过 Question 提供的方法获取集合对象
        vo.setOptions(question.getOptionsMap());
        vo.setCorrectAnswers(question.getCorrectAnswerList());
        vo.setKnowledgePoints(question.getKnowledgePointList());

        // QuestionVo 特有字段（Question 中没有）
        vo.setStudentAnswer(null); // 默认设为 null，可根据业务场景补充

        return vo;
    }

    /**
     * QuestionVo 转 Question
     */
    public Question convertToQuestion(QuestionVo vo) {
        if (vo == null) {
            return null;
        }

        Question question = new Question();
        question.setId(vo.getId());
        question.setQuestionText(vo.getQuestionText());
        question.setCourseId(vo.getCourseId());
        question.setType(vo.getType());
        question.setDifficulty(vo.getDifficulty());
        question.setExplanation(vo.getExplanation());
        question.setScore(vo.getScore());
        question.setRemarks(vo.getRemarks());
        question.setMediaUrl(vo.getMediaUrl());
        question.setCreatedAt(vo.getCreatedAt());
        question.setUpdatedAt(vo.getUpdatedAt());

        // 将集合对象转换为 JSON 字符串
        question.setOptions(convertMapToJson(vo.getOptions()));
        question.setCorrectAnswers(convertListToJson(vo.getCorrectAnswers()));
        question.setKnowledgePoints(convertListToJson(vo.getKnowledgePoints()));

        return question;
    }

    public List<String> getKnowledgePoints(String knowledgePoints) {
        if (knowledgePoints == null || knowledgePoints.isEmpty()) {
            return Collections.emptyList();
        }
        try {
            return objectMapper.readValue(knowledgePoints, new TypeReference<List<String>>() {
            });
        } catch (Exception e) {
            return Collections.emptyList(); // 出错返回空列表
        }
    }

    // 将 options 转换为 Map<String, String>
    public Map<String, String> getOptionsMap(String options) {
        if (options == null || options.isEmpty()) {
            return Collections.emptyMap();
        }
        try {
            return objectMapper.readValue(options, new TypeReference<Map<String, String>>() {
            });
        } catch (Exception e) {
            return Collections.emptyMap(); // 出错返回空映射
        }
    }


}
